<?php
namespace Dashboard\Controller;
class OrdersController extends BaseController{
    public function index(){
            $a        = (int)(I("currpage"));//跳转页数
            $sou      = I('sou');
            $content  = I('content');
            $pagesize = 15;
            if($a && $content){
                $currpage = $a;
            }else{
                $currpage = isset($_GET['currpage'])?$_GET['currpage']:1;
            }
            $_SESSION['sou'] = array("sou"=>$sou,"content"=>$content);
            $pan = I('pan');

        //参数 1 商品  2 线上报名 3攻略课堂报名
        $where = "a.type = 1 and a.del = 1";
        $table = "orders";
        $filed = "a.*,b.nickname,c.name as goods_names";
        $join  = array("a left join __USERS__ b on a.user_id = b.id","left join __GOODS__ c on a.goods_id = c.id");
            if($sou || $pan){
                $totalpage = 1;
                $currpage  = 1;
                if($sou == 1){
                    $list      = joinss($table,$filed,$join,"{$where} and c.name like '%{$content}%' and a.del = 1","a.id desc");
                }
                if($sou == 2){
                    $list      = joinss($table,$filed,$join,"{$where} and a.name like '%{$content}%' and a.del = 1","a.id desc");
                }
                if($sou == 3){
                    $list      = joinss($table,$filed,$join,"{$where} and a.phone = {$content} and a.del = 1","a.id desc");
                }
                if($sou == 4){
                    $list      = joinss($table,$filed,$join,"{$where} and a.address like '%{$content}%' and a.del = 1","a.id desc");
                }
                if($sou == 5){
                    $list      = joinss($table,$filed,$join,"a.trade_no = {$content} and a.del = 1","a.id desc");
                }
                if($pan){
                    $list      = joinss($table,$filed,$join,"{$where} and a.status = {$pan} and a.del = 1","a.id desc");
                }
            }else{
                $result = currpages($table,$filed,$join,$where,"a.id desc",$pagesize,$currpage);
                $totalpage = $result['totalpage'];
                $list      = $result['list'];
                if($result['currpage']){
                    $currpage = $result['currpage'];
                }
            }
            foreach ($list as $k=>$v){
                $list[$k]['bei_zhu'] = html_entity_decode($v['bei_zhu']);
                if($v['ji_id']){
                    $list[$k]['ji'] = $this->ji($v['ji_id']);
                }else{
                    $list[$k]['ji'] = "暂未分配";
                }
                if($v['fu_id']){
                    $list[$k]['fu'] = $this->fu($v['fu_id']);
                }else{
                    $list[$k]['fu'] = "无附加服务!";
                }
            }
        //待分配
        $dai_fen = $this->zong_he(2);
        //待付款
        $dai_fu  = $this->zong_he(1);
        //待确认
        $dai_que = $this->zong_he(3);
        //待评价
        $dai_pi  = $this->zong_he(4);
        //已完成
        $dai_yi  = $this->zong_he(5);
        $this->assign("list",$list)
            ->assign("currpage",$currpage)
            ->assign("totalpage",$totalpage)
            ->assign("dai_fen",$dai_fen)
            ->assign("dai_fu",$dai_fu)
            ->assign("dai_que",$dai_que)
            ->assign("dai_pi",$dai_pi)
            ->assign("dai_yi",$dai_yi);
        $this->display();
    }
    public function zong_he($shu){
        $dai_fen = M('orders')->where("type = 1 and status = {$shu}")->count();
        if(!$dai_fen){$dai_fen = "0";}
        return $dai_fen;
    }
    public function ji($id){
        $ji = sels("staffs","name,phone,jobs",array("id"=>array("in",$id)));
        foreach ($ji as $k=>$v){
            $arr[] = "{$v['name']}({$v['phone']})";
        }
        $list = implode(",",$arr);
        return $list;
    }
    public function fu($fu_id){
        $inf = sels("goods_fu","name,moneys",array("id"=>array("in",$fu_id)));
        foreach ($inf as $k=>$v){
            $infos[] = $v['name']."({$v['moneys']})";
        }
        $fu = implode(",",$infos);
        return $fu;
    }
    public function form(){
        $id = I('id');
        if ($id) {
            $list = joins("orders", "a.*,b.nickname,c.name as goods_names", array("a left join __USERS__ b on a.user_id = b.id", "left join __GOODS__ c on a.goods_id = c.id"), "a.id = {$id}");
            if($list['ji_id']){
                $jis = $this->ji($list['ji_id']);
                $list['ji'] = $jis;
            }
            $this->assign("list", $list);
        }else{
            //产品
            $goods = sels("goods","id,name");
        }
        if(!$goods){$goods = 1;}
        $this->assign("goods", $goods);
        $this->display();
    }
    public function add_form(){
        $id = I('id');
        if ($id) {
            $list = joins("orders", "a.*,b.nickname,c.name as goods_names", array("a left join __USERS__ b on a.user_id = b.id", "left join __GOODS__ c on a.goods_id = c.id"), "a.id = {$id}");
            if($list['ji_id']){
                $jis = $this->ji($list['ji_id']);
                $list['ji'] = $jis;
            }
            $this->assign("list", $list);
        }else{
            //产品
            $goods = sels("goods","id,name");
        }
        if(!$goods){$goods = 1;}
        $this->assign("goods", $goods);
        $this->display();
    }
    public function xiu(){
        $id = I('id');
        $ji_id = I('user_id');
        $status            = I('status');
        if($status){
            $data['status']  = $status;
        }
        if($ji_id){
            $data['status']  = 3;
            //修改技师状态
            $up_ji = up_one("staffs","id = {$ji_id}",array("status"=>2));
            //同一个订单重复分配
            $pan_orders = sel("orders","ji_id","id = {$id}");
            if($pan_orders['ji_id']){
                $pan_ji = explode(",",$pan_orders['ji_id']);
                if(in_array($ji_id,$pan_ji)){
                    $qu_ji = 1;
                }else{
                    $data['ji_id']   = $pan_orders['ji_id'].",".$ji_id;
                }
            }else{
                $data['ji_id']   = $ji_id;
            }
        }
        $numbers  = I('numbers');
        if($numbers){
            $data['numbers']  = $numbers;
        }
        $data['bei_zhu']  = I('bei_zhu');
        $data['names']  = I('names');
        $trade_no = I('trade_nos');
        if($qu_ji == 1){
            $this->success("该订单已经分配给了该技师，不可重复分配",U('form',array("id"=>$id)));
        }else{
            if($id){
                $data['up_at'] = time();
                $res = up_one("orders",array("id"=>$id),$data);
                if($res){
                    if($ji_id){
                        $ji = sel("staffs","name,phone,jobs,email","id = {$ji_id}");
                        //发送邮件
                        $orders    = sel("orders","ji_id,fu_id,tags,name,phone,address,nums,goods_id,shang_at,create_at,ding_zhu","id = {$id}");
                        $create_at = date("Y-m-d H:i:s",$orders['create_at']);
                        $shang_at  = date("Y-m-d H:i",$orders['shang_at']);
                        $goods     = sel('goods',"name","id = {$orders['goods_id']}");
                        if($orders['fu_id']){
                            $inf = sels("goods_fu","name,moneys",array("id"=>array("in",$orders['fu_id'])));
                            foreach ($inf as $k=>$v){
                                $infos[] = $v['name']."({$v['moneys']})";
                            }
                            $fu = implode(",",$infos);
                        }else{
                            $fu = "无附加服务!";
                        }
                        //技师
                        $jis = $this->ji($orders['ji_id']);
                        $content = "<br><b>服务名称：</b>{$goods['name']};
                                <hr><b>客户姓名：</b>{$orders['name']}
                                <hr><b>客户电话：</b>{$orders['phone']}
                                <hr><b>服务地址：</b>{$orders['address']}
                                <hr><b>上门时间：</b>{$shang_at}
                                <hr><b>订单内容：</b><br>附加服务：{$fu}<br>标签：{$orders['tags']}<br>
                                <hr><b>订单数量：</b>{$orders['nums']}
                                <hr><b>订单备注：</b>{$orders['ding_zhu']}
                                <hr><b>已分配技师：</b>{$jis}
                                <hr><b>订单创建时间：</b>{$create_at}";
                        $sends = think_send_mail($ji['email'],"提交需求","",$content);
                        $con   = "管理员：".$_SESSION['admin']['numbers']." 于 ".date("Y-m-d H:i:s",time()).
                            "修改了订单编号为“{$trade_no}”的订单信息,并且分配了技师（{$ji['name']}/{$ji['phone']}/{$ji['jobs']}）";
                    }else{
                        $con = "管理员：".$_SESSION['admin']['numbers']." 于 ".date("Y-m-d H:i:s",time())."修改了订单编号为“{$trade_no}”的订单信息";
                    }
                    $log     = ins("logs",array("content"=>$con,"admin_id"=>$_SESSION['admin']['id'],"create_at"=>time()));
                    $this->success("修改成功",U('index'));
                }else{
                    $this->ajaxReturn(2);
                }
            }
        }
    }

    //电话检测
    public function phone(){
        $phone = I('phone');
        $res   = sel("users","id,nickname","phone = {$phone}");
        if($res){
            $this->ajaxReturn($res['nickname']);
        }else{
            $this->ajaxReturn(1);
        }
    }
    public function add(){
        $data['bei_zhu']   = I('bei_zhu');
        $data['names']     = I('names');
        $data['numbers']   = I('numbers');
        $data['type']      = 1;
        /*$user_phone         = I('user_phone');//用户微信注册的手机号
        $u                  = sel("users","id","phone = {$user_phone}");
        $data['user_id']   = $u['id'];*/
        $data['goods_id']  = I('goods_ids');
        $data['name']       = I('a_name');
        $data['phone']      = I('a_phone');
        $data['address']    = I('a_address');
        $data['nums']       = I('nums');
        $data['moneys']     = I('moneys');
        $data['create_at']  = time();
        //随机生成订单编号
        $trade_no            = time().$data['user_id'];
        $data['trade_no']   = $trade_no;
        $ji_id                = I('user_id');
        if($ji_id){
            $data['ji_id']   = $ji_id;
            $data['status']  = 3;
            //找到订单中最后一个订单
            $res = ins("orders",$data);
            if($res){
                //修改技师状态
                $up_ji = up_one("staffs","id = {$ji_id}",array("status"=>2));
                $ji    = sel("staffs","name,phone,jobs,email","id = {$ji_id}");
                //发送邮件
                $orders    = sel("orders","ji_id,fu_id,tags,name,phone,address,nums,goods_id,shang_at,create_at,ding_zhu","trade_no = {$data['trade_no']}");
                $create_at = date("Y-m-d H:i:s",$orders['create_at']);
                $shang_at  = date("Y-m-d H:i",$orders['shang_at']);
                $goods     = sel('goods',"name","id = {$orders['goods_id']}");
                if($orders['fu_id']){
                    $inf = sels("goods_fu","name,moneys",array("id"=>array("in",$orders['fu_id'])));
                    foreach ($inf as $k=>$v){
                        $infos[] = $v['name']."({$v['moneys']})";
                    }
                    $fu = implode(",",$infos);
                }else{
                    $fu = "无附加服务!";
                }
                //技师
                $jis     = $this->ji($orders['ji_id']);
                $content = "<br><b>服务名称：</b>{$goods['name']};
                                <hr><b>客户姓名：</b>{$orders['name']}
                                <hr><b>客户电话：</b>{$orders['phone']}
                                <hr><b>服务地址：</b>{$orders['address']}
                                <hr><b>上门时间：</b>{$shang_at}
                                <hr><b>订单内容：</b><br>附加服务：{$fu}<br>标签：{$orders['tags']}<br>
                                <hr><b>订单数量：</b>{$orders['nums']}
                                <hr><b>订单备注：</b>{$orders['ding_zhu']}
                                <hr><b>已分配技师：</b>{$jis}
                                <hr><b>订单创建时间：</b>{$create_at}";
                $sends = think_send_mail($ji['email'],"提交需求","",$content);
                $con   = "管理员：".$_SESSION['admin']['numbers']." 于 ".date("Y-m-d H:i:s",time()).
                    "修改了订单编号为“{$trade_no}”的订单信息,并且分配了技师（{$ji['name']}/{$ji['phone']}/{$ji['jobs']}）";
            }
        }else{
            $data['status']  = 2;
            $res = ins("orders",$data);
            if($res){
                $con = "管理员：".$_SESSION['admin']['numbers']." 于 ".date("Y-m-d H:i:s",time())."添加了订单编号为“{$data['trade_no']}”的订单";
            }
        }
        if($res){
            $log     = ins("logs",array("content"=>$con,"admin_id"=>$_SESSION['admin']['id'],"create_at"=>time()));
            $this->success("添加成功",U('add_form'));
        }
    }

    //搜索
    public function sou(){
        $sou_one  = I('sou_one');
        $sou_two  = I('sou_two');//姓名  电话
        if($sou_two == 2){
            $where = "name like '{$sou_one}'";
        }
        if($sou_two == 3){
            $where = "phone = {$sou_one}";
        }
        $list = sels("staffs","",$where);
        if(!$list){$list = 1;}
        $this->ajaxReturn($list);
    }

    //回收箱
    public function hui(){
        $a        = (int)(I("currpage"));//跳转页数
        $sou      = I('sou');
        $content  = I('content');
        $pagesize = 15;
        if($a && $content){
            $currpage = $a;
        }else{
            $currpage = isset($_GET['currpage'])?$_GET['currpage']:1;
        }
        $pan = I('pan');
        if($sou || $pan){
            $totalpage = 1;
            $currpage  = 1;
            if($sou == 1){
                $list      = joinss("orders","a.*,b.nickname,c.name as goods_names",array("a left join __USERS__ b on a.user_id = b.id","left join __GOODS__ c on a.goods_id = c.id"),"c.name like '%{$content}%' and a.del = 2","a.id desc");
            }
            if($sou == 2){
                $list      = joinss("orders","a.*,b.nickname,c.name as goods_names",array("a left join __USERS__ b on a.user_id = b.id","left join __GOODS__ c on a.goods_id = c.id"),"a.name like '%{$content}%' and a.del = 2","a.id desc");
            }
            if($sou == 3){
                $list      = joinss("orders","a.*,b.nickname,c.name as goods_names",array("a left join __USERS__ b on a.user_id = b.id","left join __GOODS__ c on a.goods_id = c.id"),"a.phone = {$content} and a.del = 2","a.id desc");
            }
            if($sou == 4){
                $list      = joinss("orders","a.*,b.nickname,c.name as goods_names",array("a left join __USERS__ b on a.user_id = b.id","left join __GOODS__ c on a.goods_id = c.id"),"a.address like '%{$content}%' and a.del = 2","a.id desc");
            }
            if($sou == 5){
                $list      = joinss("orders","a.*,b.nickname,c.name as goods_names",array("a left join __USERS__ b on a.user_id = b.id","left join __GOODS__ c on a.goods_id = c.id"),"a.trade_no = {$content} and a.del = 2","a.id desc");
            }
            if($pan){
                $list      = joinss("orders","a.*,b.nickname,c.name as goods_names",array("a left join __USERS__ b on a.user_id = b.id","left join __GOODS__ c on a.goods_id = c.id"),"a.status = {$pan} and a.del = 2","a.id desc");
            }
        }else{
            $result = currpages("orders","a.*,b.nickname,c.name as goods_names",array("a left join __USERS__ b on a.user_id = b.id","left join __GOODS__ c on a.goods_id = c.id"),"a.del = 2","a.id desc",$pagesize,$currpage);
            $totalpage = $result['totalpage'];
            $list      = $result['list'];
            if($result['currpage']){
                $currpage = $result['currpage'];
            }
        }
        foreach ($list as $k=>$v){
            $list[$k]['bei_zhu'] = html_entity_decode($v['bei_zhu']);
            if($v['ji_id']){
                $list[$k]['ji'] = $this->ji($v['ji_id']);
            }else{
                $list[$k]['ji'] = "暂未分配";
            }
            if($v['fu_id']){
                $list[$k]['fu'] = $this->fu($v['fu_id']);
            }else{
                $list[$k]['fu'] = "无附加服务!";
            }
        }
        $this->assign("list",$list)->assign("currpage",$currpage)->assign("totalpage",$totalpage);
        $this->display();
    }
    //用户评价
    public function ping(){
        $a        = (int)(I("currpage"));//跳转页数
        $sou      = I('sou');
        $content  = I('content');
        $pagesize =20;
        if($a && $content){
            $currpage = $a;
        }else{
            $currpage = isset($_GET['currpage'])?$_GET['currpage']:1;
        }

        //参数
        $where = "";
        $table = "comment_goods";
        $filed = "a.id,a.content,a.xing_one,a.xing_two,a.create_at,b.name as user_name,b.phone as user_phone,b.avatar,c.name as goods_name,d.nums,d.trade_no,d.moneys,d.name as orders_name,d.phone as orders_phone";
        $join  = array("a left join __USERS_JIINFO__ b on a.user_id = b.id","left join __GOODS__ c on a.goods_id = c.id","left join __ORDERS__ d on a.orders_id = d.id");
        if($sou && $content){
            $totalpage = 1;
            $currpage  = 1;
            if($sou == 1){
                $list      = joinss($table,$filed,$join,"c.name like '%{$content}%'","a.id desc");
            }
            if($sou == 2){
                $list      = joinss($table,$filed,$join,"b.name like '%{$content}%' or d.name like '%{$content}%'","a.id desc");
            }
            if($sou == 3){
                $list      = joinss($table,$filed,$join,"b.phone like '%{$content}%' or d.phone like '%{$content}%'","a.id desc");            }
            if($sou == 4){
                $list      = joinss($table,$filed,$join,"d.trade_no like '%{$content}%'","a.id desc");
            }
        }else{
            $result = currpages($table,$filed,$join,"","a.id desc",$pagesize,$currpage);
            $totalpage = $result['totalpage'];
            $list      = $result['list'];
            if($result['currpage']){
                $currpage = $result['currpage'];
            }
        }
        $this->assign("list",$list)->assign("currpage",$currpage)->assign("totalpage",$totalpage);
        $this->display();
    }
    //订单流水
    public function stat(){
        //订单总数
        $orders_z = M("orders")->count();
        if(!$orders_z){$orders_z= "0";}
        //支付订单数
        $orders_fu = M("orders")->where("status > 1")->count();
        if(!$orders_fu){$orders_fu= "0";}
        //待支付订单数
        $orders_wei = M("orders")->where("status = 1")->count();
        if(!$orders_wei){$orders_wei= "0";}
        //总流水
        $orders_sum = M("orders")->sum("moneys");
        if(!$orders_sum){$orders_sum= "0";}
        //已支付流水
        $orders_yi = M("orders")->where("status > 1")->sum("moneys");
        if(!$orders_yi){$orders_yi= "0";}
        //待付款流水
        $orders_dai = M("orders")->where("status = 1")->sum("moneys");
        if(!$orders_dai){$orders_dai= "0";}
        $this->assign("zong",$orders_z)->assign("fu",$orders_fu)->assign("sum",$orders_sum)->assign("wei",$orders_wei)->assign("yi",$orders_yi)->assign("dai",$orders_dai);
        $this->display();
    }
    public function shan(){
        $start = strtotime($_POST['start']);
        $end   = strtotime($_POST['end']);
        if($start <= $end && $start != "" && $end != ""){
            //订单总数
            $orders_z = M("orders")->where("create_at >= {$start} and create_at < {$end}")->count();
            if(!$orders_z){$orders_z= "0";}
            //支付订单数
            $orders_fu = M("orders")->where("status > 1 and create_at >= {$start} and create_at < {$end}")->count();
            if(!$orders_fu){$orders_fu= "0";}
            //待支付订单数
            $orders_wei = M("orders")->where("status = 1 and create_at >= {$start} and create_at < {$end}")->count();
            if(!$orders_wei){$orders_wei= "0";}
            //总流水
            $orders_sum = M("orders")->where("create_at >= {$start} and create_at < {$end}")->sum("moneys");
            if(!$orders_sum){$orders_sum= "0";}
            //已支付流水
            $orders_yi = M("orders")->where("status > 1 and create_at >= {$start} and create_at < {$end}")->sum("moneys");
            if(!$orders_yi){$orders_yi= "0";}
            //待付款流水
            $orders_dai = M("orders")->where("status = 1 and create_at >= {$start} and create_at < {$end}")->sum("moneys");
            if(!$orders_dai){$orders_dai= "0";}
            $this->ajaxReturn(array("zong"=>$orders_z,"fu"=>$orders_fu,"sum"=>$orders_sum,"wei"=>$orders_wei,"yi"=>$orders_yi,"dai"=>$orders_dai));
        }else{
            $this->ajaxReturn(3);
        }
    }
}